package com.abbas.abchat.dao.impl;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.abbas.abchat.dao.FriendRoundDao;
import com.abbas.abchat.db.Sqlite;
import com.abbas.abchat.entity.FriendRound;

import java.util.ArrayList;
import java.util.List;

/**
 * @author abbas
 * @description: TODO
 * @date 2022/4/19 14:18
 */
public class FriendRoundImpl implements FriendRoundDao {

    private Context mContext;
    private SQLiteDatabase sqLiteDatabase;

    public FriendRoundImpl(Context context){
        mContext = context;
        sqLiteDatabase = new Sqlite(context, Sqlite.DB_NAME, null, 1).getWritableDatabase();
    }

    @Override
    public List<FriendRound> findAll() throws Exception {
        return null;
    }

    @Override
    public FriendRound findById(int _id) throws Exception {
        return null;
    }

    @Override
    public long insert(FriendRound entity) throws Exception {
        ContentValues contentValues = new ContentValues();
        contentValues.put(FriendRound.P_ID,entity.getpId());
        contentValues.put(FriendRound.PUB_DATE,entity.getPubDate());
        contentValues.put(FriendRound.LOCATION,entity.getLocation());
        contentValues.put(FriendRound.CONTENT,entity.getContent());
        return sqLiteDatabase.insert(FriendRound.DB_Name, null, contentValues);
    }

    @Override
    public int deleteById(int _id) throws Exception {
        return 0;
    }

    @Override
    public int updateById(FriendRound entity) throws Exception {
        return 0;
    }

    //in ?
    @SuppressLint("Range")
    @Override
    public List<FriendRound> findByListIds(List<Integer> ids) throws Exception {
        List<FriendRound> rounds = new ArrayList<>();
        Cursor cursor = null;
        for (Integer id : ids) {
            cursor = sqLiteDatabase.query(FriendRound.DB_Name,null,FriendRound.P_ID+" = ?",new String[]{id+""},null,null,null);
            while (cursor.moveToNext()){
                FriendRound friendRound = new FriendRound();
                friendRound.setId(cursor.getInt(cursor.getColumnIndex(FriendRound.ID)));
                friendRound.setpId(cursor.getInt(cursor.getColumnIndex(FriendRound.P_ID)));
                friendRound.setPubDate(cursor.getString(cursor.getColumnIndex(FriendRound.PUB_DATE)));
                friendRound.setContent(cursor.getString(cursor.getColumnIndex(FriendRound.CONTENT)));
                friendRound.setLocation(cursor.getString(cursor.getColumnIndex(FriendRound.LOCATION)));
                rounds.add(friendRound);
            }
        }
        if (cursor != null){
            cursor.close();
        }
        return rounds;
    }
}
